/**
 * 
 */
package neptune.web.client.view;

import neptune.web.client.controller.UserViewController;
import neptune.web.client.services.ApplicationDataProvider;
import neptune.web.client.view.components.UsernameTableRenderer;

import org.gwtlib.client.table.ColumnLayout;
import org.gwtlib.client.table.ui.Column;
import org.gwtlib.client.table.ui.PagingBar;
import org.gwtlib.client.table.ui.PagingTable;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class UserView extends View {

	public static final String DEFAULT_TOKEN = "UserSummary";
	private PagingTable table;

	public UserView() {

		// Setting view params
		try {
			this.setName(DEFAULT_TOKEN);
		} catch (DuplicatedTokenException e) {
			ConsoleManager.addConsoleDebugMessage(e.getMessage(), true);
		}

		Column[] colums = {
				new Column(0, true, "Username", "33%",
						new UsernameTableRenderer()),
				new Column(1, true, "Role", "33%"),
				new Column(2, true, "e-mail", "33%") };

		ColumnLayout layout = new ColumnLayout(colums);

		int usrNumber = ApplicationDataProvider.getInstance()
				.getUsersNumber();

		table = new PagingTable(layout, new PagingBar(usrNumber, 10));
		table.setContentProvider(new UserViewController(table));
		table.setSize(10);

		table.setWidth("90%");

		VerticalPanel panel = new VerticalPanel();
		panel.add(table);
		Frame frame = new Frame(GWT.getModuleBaseURL() + "UserSummary.html");
		panel.add(frame);
		frame.setHeight("300px");
		frame.setWidth("90%");
		panel.setCellWidth(frame, "100%");

		panel.addStyleName("UserSummaryView");
		frame.addStyleName("UserSummaryView-frame");

		initWidget(panel);
	}

	public void update() {
		// Ridisegna la tabella
		table.getPagingBar().setSize(
				ApplicationDataProvider.getInstance().getUsersNumber());
		table.getPagingBar().update();
		table.update();
	}

	public void reset() {
		// Azzera la cache della tabella
		table.reset();
	}
}
